<script setup lang="ts">
import { onMounted, reactive, ref } from "vue";
import MdEditor from "@/components/MdEditor.vue";
import {
  QuestionAddRequest,
  QuestionControllerService,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRoute } from "vue-router";

const route = useRoute();
const form = reactive({
  id: "" as any,
  answer: "",
  content: "",
  judgeCase: [
    {
      input: "",
      output: "",
    },
  ],
  judgeConfig: {
    memoryLimit: 0,
    stackLimit: 0,
    timeLimit: 0,
  },
  tags: [],
  title: "",
});

const editPage = route.path.includes("edit");
const loadData = async () => {
  var id = route.query?.id;
  if (!id) {
    return;
  }
  const res = await QuestionControllerService.getQuestionByIdUsingGet(
    id as any
  );
  if (res.code === 0) {
    form.id = res?.data?.id as any;
    form.title = res?.data?.title || "无";
    form.content = res?.data?.content || "无";
    form.answer = res?.data?.answer || "无";
    form.tags = JSON.parse(res?.data?.tags as any) || [];
    form.judgeConfig = JSON.parse(res?.data?.judgeConfig as string) || {
      memoryLimit: 0,
      stackLimit: 0,
      timeLimit: 0,
    };
    form.judgeCase = JSON.parse(res?.data?.judgeCase as string) || [
      {
        input: "",
        output: "",
      },
    ];
    console.log(form.judgeCase);
  } else {
    message.error(res.message as string);
  }
};
onMounted(() => {
  loadData();
});
const handleAdd = () => {
  form.judgeCase?.push({
    input: "",
    output: "",
  });
};
const handleDelete = (index: number) => {
  form.judgeCase?.splice(index, 1);
};

const doSubmit = async () => {
  if (editPage) {
    const res = await QuestionControllerService.updateQuestionUsingPost(form);
    if (res.code === 0) message.success("更新成功");
    else message.error("更新失败");
  } else {
    const res = await QuestionControllerService.addQuestionUsingPost(form);
    if (res.code === 0) message.success("添加成功");
    else message.error("添加失败");
  }
};

const answerChange = (val: string) => {
  form.answer = val;
};

const contentChange = (val: string) => {
  form.content = val;
};
</script>

<template>
  <div>
    <h1>添加题目</h1>
    <a-form :model="form" label-align="left">
      <a-form-item field="title" label="标题">
        <a-input v-model="form.title" placeholder="请输入题目标题" />
      </a-form-item>
      <a-form-item field="tags" label="标签">
        <a-input-tag v-model="form.tags" placeholder="请选择标签" allow-clear />
      </a-form-item>
      <a-form-item field="answer" label="题目答案">
        <MdEditor :value="form.answer" :handleChange="answerChange"></MdEditor>
      </a-form-item>
      <a-form-item field="content" label="题目内容">
        <MdEditor
          :value="form.content"
          :handleChange="contentChange"
        ></MdEditor>
      </a-form-item>

      <a-form-item label="判题配置" :content-flex="false" :merge-props="false">
        <a-space direction="vertical" style="min-width: 480px">
          <a-form-item
            field="judgeConfig.timeLimit"
            validate-trigger="input"
            min="0"
            label="时间限制"
          >
            <a-input-number
              v-model="form.judgeConfig.timeLimit"
              placeholder="请输入时间限制"
              mdoe="button"
              min="0"
              size="large"
            />
          </a-form-item>
          <a-form-item
            field="judgeConfig.stackLimit"
            validate-trigger="input"
            min="0"
            label="堆栈限制"
          >
            <a-input-number
              v-model="form.judgeConfig.stackLimit"
              placeholder="请输入堆栈限制"
              mdoe="button"
              size="large"
            />
          </a-form-item>
          <a-form-item
            field="judgeConfig.momeryLimit"
            validate-trigger="input"
            label="内存限制"
          >
            <a-input-number
              v-model="form.judgeConfig.memoryLimit"
              placeholder="请输入内存限制"
              mdoe="button"
              size="large"
            />
          </a-form-item>
        </a-space>
      </a-form-item>
      <a-form-item
        label="测试用例"
        :content-flex="false"
        :merge-props="false"
      ></a-form-item>
      <a-form-item
        v-for="(judgeCaseItem, index) of form.judgeCase"
        :field="'测试用例  ${index}'"
        :key="index"
      >
        <a-space direction="vertical" style="min-width: 480px">
          <a-form-item
            :field="`form.judgeCase[${index}].input`"
            :label="`输入用例-${index}`"
            :key="index"
          >
            <a-input
              v-model="judgeCaseItem.input"
              placeholder="请输入测试用例输入"
            />
          </a-form-item>
          <a-form-item
            :field="`form.judgeCase[${index}].value`"
            :label="`输出用例-${index}`"
            :key="index"
          >
            <a-input
              v-model="judgeCaseItem.output"
              placeholder="请输入测试用例输出"
            />
          </a-form-item>
          <a-button status="danger" @click="handleDelete(index)"
            >删除测试用例
          </a-button>
        </a-space>
      </a-form-item>
      <a-form-item>
        <div>
          <a-button type="outline" status="success" @click="handleAdd"
            >新增测试用例
          </a-button>
        </div>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" style="min-width: 200px" @click="doSubmit()"
          >创建题目
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped></style>
